package cn.ewan.cloud.tenant.data.router.core.business.plugin.db.mysql.sharding;

import cn.ewan.cloud.tenant.data.router.core.business.plugin.db.TenantKeyBuilder;
import org.apache.commons.lang3.StringUtils;

/**
 * @author lgh
 * @date 2023/3/13 16:47
 */
public class MysqlKeyPlugin extends TenantKeyBuilder {

    /**
     * 标记符号
     */
    public static final String FLAG = "--";


    @Override
    protected String buildTenantActualTag(String actualTag, String tenant) {
        return tenant + FLAG + actualTag;
    }


    @Override
    protected String getActualTag(String logicTag) {
        if (!isMathTenantRule(logicTag)) {
            return null;
        }
        String[] actualNames = logicTag.split(FLAG);
        return actualNames.length >= 2 ? actualNames[1] : null;
    }


    @Override
    protected boolean isMathTenantRule(String logicTag) {
        return StringUtils.isNotBlank(logicTag) && logicTag.contains(FLAG);
    }
}
